package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.ResponseUtil;
import org.apache.commons.io.FilenameUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.0
 * @auther Administrator
 * @date 2023/2/22 15:00
 * 上传文件
 */
@RequestMapping("/admin/storage")
@RestController
@CrossOrigin
public class StorageController {


    @Autowired
    private DtsStorageService dtsStorageService;

    /**
     * 上传图片
     * {
     * 	"errno": 0,
     * 	"data": {
     * 		"id": 34,
     * 		"key": "dd75ec03-8f88-447b-a925-4e7f35a299f4.jpg",
     * 		"name": "16.jpg",
     * 		"type": ".jpg",
     * 		"size": 35012,
     * 		"url": "http://qianfeng-file.oss-cn-beijing.aliyuncs.com/dd75ec03-8f88-447b-a925-4e7f35a299f4.jpg?Expires=1622471984&OSSAccessKeyId=LTAI4GCjGC58aGgnLjd5hixp&Signature=%2Bu%2BczJdE4I6jW4DagLVYT0Ps3dw%3D",
     * 		"addTime": 1621391984382,
     * 		"updateTime": 1621391984382,
     * 		"deleted": false
     *        },
     * 	"errmsg": "成功"
     * }
     */
    @RequiresPermissions(value = "admin:storage:create")
    @RequiresPermissionsDesc(menu = {"系统管理","对象存储"},button = "添加")
    @PostMapping("/create")
    public Object create(MultipartFile file) throws Exception{

        String ext = FilenameUtils.getExtension(file.getOriginalFilename());
        String filename = file.getOriginalFilename();

        //开始上传
        DtsStorage dtsStorage = dtsStorageService.create(
                ext,(int) file.getSize(),file.getBytes(),filename);
        return ResponseUtil.ok(dtsStorage);
    }

    /**
     * 查看图片
     * 接口地址 : http://localhost:8083/admin/storage/list?page=1&limit=20&sort=add_time&order=desc
     * @param key
     * @param page
     * @param limit
     * @return
     */
    @RequiresPermissions(value = "admin:storage:list")
    @RequiresPermissionsDesc(menu = {"系统管理","对象存储"},button = "查询")
    @GetMapping("/list")
    public Object list(
            String key,
            String name,
            String sort,
            String order,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer limit
    ){
        IPage<DtsStorage> iPage = dtsStorageService.findListByQuery(key,name,sort,order,page,limit);
        Map data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());
        return ResponseUtil.ok(data);
    }

    /**
     * 更新
     * @param dtsStorage
     * @return
     */
    @RequiresPermissions(value = "admin:storage:update")
    @RequiresPermissionsDesc(menu = {"系统管理","对象存储"},button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody DtsStorage dtsStorage){
        dtsStorage = dtsStorageService.update(dtsStorage);
        return ResponseUtil.ok(dtsStorage);
    }

    /**
     * 删除
     * @param dtsStorage
     * @return
     */
    @RequiresPermissions(value = "admin:storage:delete")
    @RequiresPermissionsDesc(menu = {"系统管理","对象存储"},button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsStorage dtsStorage){

        dtsStorage = dtsStorageService.delete(dtsStorage);
        return ResponseUtil.ok(dtsStorage);
    }

}
